// pages/homePage/homePage.js
var app = getApp();
var util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {

    // indicatorDots: true,
    // autoplay: true,
    // interval: 2000,
    // duration: 1000,
    // productType: []
       index: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // this.getUserLocation();
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight
    })
    /**
 * 获取4个类
 */
    wx.request({
      url: app.globalData.url + "getWaitJoinGroupShopping",
      method: "GET",
      data: {
      },
      success: function (res) {
        if (res.data.result == 1) {
          var endTimeList = new Array();
          for (var i = 0; i < res.data.data.length; i++) {
            endTimeList[i] = res.data.data[i].endTime;
          }
          that.setData({
            groups: res.data.data,
          })
          that.getTimeList(endTimeList)
        } else {
          that.setData({
            groups: 0
          })
        }
      }
    });
  },
  /**
   * 页面分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.title,
      desc: '分享页面的内容',
      imageUrl: "http://file.jwsxmall.com/shareImg.jpg",
      path: '/pages/homePage/homePage?partnerUserId=' + wx.getStorageSync("userId") + '&type=2', // 路径，传递参数到指定页面
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

  },
  getTimeList: function (endTimeList) {
    var that = this;
    var daysList = new Array();
    var hoursList = new Array();
    var minutesList = new Array();
    var secondsList = new Array();
    for (var i = 0; i < endTimeList.length; i++) {
      var sdfDate = endTimeList[i].replace(/\.|\-/g, "/");
      var totalSeconds = new Date(sdfDate) - new Date();
      var days = Math.floor(totalSeconds / (24 * 3600 * 1000));
      var leave1 = totalSeconds % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
      var hours = Math.floor(leave1 / (3600 * 1000));
      if (hours < 10) {
        hours = '0' + hours;
      }
      var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
      var minutes = Math.floor(leave2 / (60 * 1000));
      if (minutes < 10) {
        minutes = '0' + minutes;
      }
      var leave3 = leave2 % (60 * 1000);
      var seconds = Math.floor(leave3 / 1000);
      if (seconds < 10) {
        seconds = '0' + seconds;
      }
      daysList[i] = days;
      hoursList[i] = hours;
      minutesList[i] = minutes;
      secondsList[i] = seconds;
    }
    that.setData({
      daysList: daysList,
      hoursList: hoursList,
      minutesList: minutesList,
      secondsList: secondsList,
    })
    that.interval();
  },
  // 计时器
  interval: function () {
    const that = this;
    if (that.data.groups != undefined) {
      let timer = setInterval(function () {  // 设置定时器
        var secondsList = that.data.secondsList;
        var minutesList = that.data.minutesList;
        var hoursList = that.data.hoursList;
        var daysList = that.data.daysList;
        for (var i = 0; i < that.data.groups.length; i++) {
          var seconds = parseInt(secondsList[i]);
          var minutes = parseInt(minutesList[i]);
          var hours = parseInt(hoursList[i]);
          var days = parseInt(daysList[i]);
          seconds--;
          if (seconds <= 0) {
            seconds = 60;
            minutes--;
            if (minutes <= 0) {
              minutes = 59;
              hours--;
              if (hours <= 0) {
                hours = 23;
                days--;
              }
              if (days < 0) {
                clearInterval(timer);
                that.onLoad();
              }
            }
            if (minutes < 10) {
              minutesList[i] = '0' + minutes;
              that.setData({
                minutesList: minutesList
              })
            } else {
              minutesList[i] = minutes;
              that.setData({
                minutesList: minutesList
              })
            }

          }

          if (seconds < 10) {
            // 少于10补零
            secondsList[i] = '0' + seconds;
            that.setData({
              secondsList: secondsList
            })
          } else {
            secondsList[i] = seconds;
            that.setData({
              secondsList: secondsList
            })
          }
          daysList[i] = days;
          that.setData({
            daysList: daysList
          })
          hoursList[i] = hours;
          that.setData({
            hoursList: hoursList
          })
        }
      }, 1000)
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  /**
   * 跳转到产品详情
   */
  details: function (res) {
    var productid = res.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?productid=' + productid+'&type=2',
    })

  },
  goGroup: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/assembleDetail/assembleDetail?orderId=' + e.currentTarget.dataset.orderid + '&share=1&type=2&groupShoppingId=' + e.currentTarget.dataset.hisid
    })
  },
  navBack: function () {
    wx.navigateBack({

    })
  }
})